package com.tl.commonlibrary.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tl.commonlibrary.R;
import com.tl.commonlibrary.tool.DeviceUtil;

/**
 * Created by Administrator on 2017/9/21 0021.
 */

public class CustomAlertDialog extends Dialog implements View.OnClickListener {
    private TextView rightBtn, leftBtn;
    private OnAlertClickListener listener;
    private TextView contentTView;
    private String content, leftText, rightText;

    public CustomAlertDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_alert);
        Window window = getWindow();
        WindowManager manager = window.getWindowManager();
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = window.getAttributes();
        Point point = new Point();
        display.getSize(point);
//        params.height = Math.min(DeviceUtil.dp_to_px(200, getContext()), (int) (point.y * 0.5));
        params.width = Math.min(DeviceUtil.dp_to_px(300, getContext()), (int) (point.y * 2.0 / 3));
        window.setAttributes(params);
        contentTView = (TextView) findViewById(R.id.contentTView);
        if (!TextUtils.isEmpty(content)) {
            contentTView.setText(content);
        }
        rightBtn = (TextView) findViewById(R.id.rightBtn);
        rightBtn.setOnClickListener(this);
        if (!TextUtils.isEmpty(rightText)) {
            rightBtn.setText(rightText);
        }
        leftBtn = (TextView) findViewById(R.id.leftBtn);
        leftBtn.setOnClickListener(this);
        if (!TextUtils.isEmpty(leftText)) {
            leftBtn.setText(leftText);
        }
        setCancelable(false);
    }

    public CustomAlertDialog setContent(String content) {
        this.content = content;
        if (contentTView != null) {
            contentTView.setText(content);
        }
        return this;
    }

    public CustomAlertDialog setLeftText(String content) {
        this.leftText = content;
        if (leftBtn != null) {
            leftBtn.setText(content);
        }
        return this;
    }

    public CustomAlertDialog setRightText(String content) {
        this.rightText = content;
        if (rightBtn != null) {
            rightBtn.setText(content);
        }
        return this;
    }

    public void setListener(OnAlertClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rightBtn) {
            dismiss();
            if (listener != null) {
                listener.onRightClicked(v);
            }
        } else if (v.getId() == R.id.leftBtn) {
            dismiss();
            if (listener != null) {
                listener.onLeftClicked(v);
            }
        }
    }

    public interface OnAlertClickListener {
        void onRightClicked(View view);

        void onLeftClicked(View view);
    }

}